# delimit;
clear;

use ${datapath}\household_d.dta;

local control "av_age numage numwomen tnwginc educ resp_women sc";

* water22015 number of water supply facilities;
* aggregate index, considering before 2015;

/* On public goods */;
eststo clear;
qui eststo:reg water_access res_w  i_water2_bf05 `control' gp1-gp50,cluster(gpname) ;
qui eststo:ivregress 2sls water_access (res_w=random) i_water2_bf05 `control' gp1-gp50,cluster(gpname) ;
estimates store water;

foreach x in stlit road draig  {;
qui eststo:reg `x'2015 res_w v_`x' `control' gp1-gp50 ,cluster(gpname) ;
gen sam`x'=e(sample);
qui eststo:ivregress 2sls `x'2015 (res_w=random) v_`x' `control' gp1-gp50 ,cluster(gpname) ;
estimates store `x';
};

esttab, b(3) t(2) keep(res_w ) se  compress mtitles("Access" "Access" "Street" "Street" "Road" "Road" "Drainage" "Drainage") stats(ymean N, labels  ("Mean" "Observations") fmt(2 0)) star(+ 0.10 * 0.05 ** 0.01);
esttab using ${outpath}\water_agg.csv, replace label b(3) t(2) keep(res_w) se  compress mtitles("Access" "Access" "Street" "Street" "Road" "Road" "Drainage" "Drainage") 
stats(ymean N, labels ("Mean" "Observations") fmt(2 0)) star(+ 0.10 * 0.05 ** 0.01) fragment;
eststo clear;

coefplot (water) (stlit) (road) (draig), legend(off) keep(res_w) xline(0) ylab(0.70 "Water" 0.9 "Street light" 1.1 "Road" 1.3 "Drainage" );

/*;
rwolf water_access stlit road draig water_repar stlitrepar roadrepar draigrepar 
, indepvar(res_w)  method(ivregress) iv(random) controls(`control' i_water2_bf05 gp1-gp50) vce(cluster gpname);*/

/* first stage */;
eststo clear;
qui eststo:reg  res_w random i_water2_bf05 `control' gp1-gp50 ,cluster(gpname) ;
qui eststo:reg  res_w random v_stlit `control' gp1-gp50 if samstlit==1,cluster(gpname) ;
qui eststo:reg  res_w random v_road `control' gp1-gp50 if samroad==1,cluster(gpname) ;
qui eststo:reg  res_w random v_draig `control' gp1-gp50 if samdraig==1,cluster(gpname) ;

esttab, b(3) t(2) keep(random ) se  compress  stats(ymean N, labels   ("Mean" "Observations") fmt(2 0)) star(+ 0.10 * 0.05 ** 0.01);
esttab using "${outpath}\first_stage.csv", replace label b(3) t(2) keep(random) se  compress mtitles("Access" "Access" "Street" "Street" "Road" "Road" "Drainage" "Drainage") 
stats(ymean N, labels ("Mean" "Observations") fmt(2 0)) star(+ 0.10 * 0.05 ** 0.01) fragment;
eststo clear;

/* members' affiliation and public goods */;

qui eststo:ivregress 2sls water_access (res_w=random) i_water2_bf05 `control'  age_m educ_m exp_self gp1-gp50,cluster(gpname) ;
qui eststo:ivregress 2sls stlit2015 (res_w=random) v_stlit `control' educ_m age_m exp_self gp1-gp50,cluster(gpname) ;
qui eststo:ivregress 2sls road2015 (res_w=random) v_road `control' educ_m age_m exp_self gp1-gp50,cluster(gpname) ;
qui eststo:ivregress 2sls draig2015 (res_w=random) v_draig `control' educ_m age_m exp_self gp1-gp50,cluster(gpname) ;

esttab, t(2) keep(res_w  educ_m  age_m exp_self) se  compress mtitles("water" "street" "road" "drainage") star(+ 0.10 * 0.05 ** .01) b(3);
esttab using "${outpath}\productivity.csv", replace label b(3) keep(res_w  educ_m  age_m exp_self) se compress mtitles("water""street" "road" "drainage") 
stats( N, labels ( "Observations") fmt(2 0)) star(+ 0.10 * 0.05 ** 0.01) fragment;
eststo clear;

/* robustness: using respondents without knowledge about reservation status */;
preserve;
keep if aware_r==0;
qui eststo:reg water_access res_w  i_water2_bf05 `control' gp1-gp50,cluster(gpname) ;
qui eststo:ivregress 2sls water_access (res_w=random) i_water2_bf05 `control' gp1-gp50,cluster(gpname) ;

foreach x in stlit road draig  {;
qui eststo:reg `x'2015 res_w v_`x' `control' gp1-gp50 ,cluster(gpname) ;
qui eststo:ivregress 2sls `x'2015 (res_w=random) v_`x' `control' gp1-gp50 ,cluster(gpname) ;
};

esttab, b(3) t(2) keep(res_w ) se  compress mtitles("Access" "Access" "Street" "Street" "Road" "Road" "Drainage" "Drainage") stats(ymean N, labels   ("Mean" "Observations") fmt(2 0)) star(+ 0.10 * 0.05 ** 0.01);
esttab using "${outpath}\water_agg_robust.csv", replace label b(3) t(2) keep(res_w) se  compress mtitles("Access" "Access" "Street" "Street" "Road" "Road" "Drainage" "Drainage") 
stats(ymean N, labels ("Mean" "Observations") fmt(2 0)) star(+ 0.10 * 0.05 ** 0.01) fragment;
eststo clear;
restore;

* gender/castes combination ;
qui eststo:ivregress 2sls water_access (res_w=random) p_res_w same i_water2_bf05 `control' gp1-gp50,cluster(gpname) ;
qui eststo:ivregress 2sls water_access (res_w=random) p_res_c same_c i_water2_bf05 `control' gp1-gp50,cluster(gpname) ;

foreach x in stlit road draig  {;
qui eststo:ivregress 2sls `x'2015 (res_w=random) v_`x' p_res_w same gp1-gp50 `control',cluster(gpname) ;
qui eststo:ivregress 2sls `x'2015 (res_w=random) v_`x' p_res_c same_c gp1-gp50 `control',cluster(gpname) ;
};;

esttab, b(3) t(2) keep(res_w p_res_w p_res_c same same_c) se  compress mtitles("water""water""street" "street" "road" "road" "draig" "draig") stats(ymean N, labels   ("Mean" "Observations") fmt(2 0)) star(+ 0.10 * 0.05 ** 0.01);
esttab using "${outpath}\same.csv", replace label b(3) t(2) keep(res_w p_res_w p_res_c same same_c) se  compress mtitles("water""water""street" "street" "road" "road" "draig" "draig") stats(ymean N, labels ("Mean" "Observations") fmt(2 0)) star(+ 0.10 * 0.05 ** 0.01) fragment;
eststo clear;

* president;
qui eststo:reg water_access president_f i_water2_bf05 `control' i.taluk ,cluster(gpname) ;
foreach x in stlit road draig  {;
qui eststo:reg `x'2015 president_f v_`x' `control' i.taluk,cluster(gpname) ;};

esttab, b(3) t(2) keep(president_f) se  compress mtitles("water""street""road""draig") stats(ymean N, labels   ("Mean" "Observations") fmt(2 0)) star(+ 0.10 * 0.05 ** 0.01);
esttab using "${outpath}\president.csv", replace label b(3) t(2) keep(president_f) se  compress mtitles("water""street""road""draig") stats(ymean N, labels ("Mean" "Observations") fmt(2 0)) star(+ 0.10 * 0.05 ** 0.01) fragment;
eststo clear;

* each water supply facility;
foreach x in mws psp  ro phi  {;
qui eststo:ivregress 2sls `x'2015 (res_w=random)  `x'bf2015 `control' gp1-gp50,cluster(gpname) ;
qui estadd ysumm;
};
esttab, b(3) t(2) keep(res_w ) se  compress mtitles("Mini" "Public" "RO" "Private"  ) stats(ymean N, labels  ("Mean" "Observations") fmt(2 0)) star(+ 0.10 * 0.05 ** 0.01);
esttab using "${outpath}\water.csv", replace label b(3) t(2) keep(res_w) se  compress mtitles("Mini" "Public""RO" "Private" ) stats(ymean N, labels ("Mean" "Observations") 
fmt(2 0)) star(+ 0.10 * 0.05 ** 0.01) fragment;
eststo clear;

* repair;
foreach x in water_ stlit road draig{;
eststo:qui ivregress 2sls `x'repar (res_w=random) gp1-gp50 `control',cluster(gpname);
qui estadd ysumm;
};
esttab, b(3) t(2) keep(res_w ) se  compress mtitles("water" "Lights" "Road"  "Drainage") stats(ymean N, labels  ("Mean" "Observations") fmt(2 0)) star(+ 0.10 * 0.05 ** 0.01);
esttab using "${outpath}\water_r.csv", replace label b(3) t(2) keep(res_w) se  compress mtitles("water" "Lights" "Road"  "Drainage") stats(ymean N, labels ("Mean" "Observations") 
fmt(2 0)) star(+ 0.10 * 0.05 ** 0.01) fragment;
eststo clear;

* who;
foreach x in water2_  stlit road draig {;
eststo:qui ivregress 2sls `x'gp (res_w=random) gp1-gp50 `control',cluster(gpname);
qui estadd ysumm;
eststo:qui ivregress 2sls `x'gp2 (res_w=random) gp1-gp50 `control',cluster(gpname);
qui estadd ysumm;
};
esttab, b(3) t(2) keep(res_w) se  compress mtitles( "water""water"  "Lights""Lights" "Road""Road"  "Drainage""Drainage") stats(ymean N, labels  ("Mean" "Observations") fmt(2 0)) star(+ 0.10 * 0.05 ** 0.01);
esttab using "${outpath}\water_m.csv", replace label b(3) t(2) keep(res_w) se  compress mtitles("water""water"  "Lights""Lights" "Road""Road"  "Drainage""Drainage") stats(ymean N, labels ("Mean" "Observations") fmt(2 0)) star(+ 0.10 * 0.05 ** 0.01) fragment;
eststo clear;


/* other benefits */;

foreach x in hou toilt nrgwage nrgpri agri oap shed {;
eststo:qui ivregress 2sls D_`x' `x'bf (res_w=random) gp1-gp50 `control',cluster(gpname);
qui estadd ysumm;
};
esttab, b(3) t(2) keep(res_w ) se  compress mtitles("Housing" "Toilet" "Wage" "Work" "Agri.""Pension""Cattle") stats(ymean N, labels ("Mean" "Observations") fmt(2 0)) star(+ 0.10 * 0.05 ** 0.01);
esttab using "${outpath}\other1.csv", replace label b(3) t(2) keep(res_w ) se  compress mtitles("Housing" "Toilet" "Wage" "Work" "Agri.""Pension""Cattle") stats(ymean N, labels ("Mean" "Observations") fmt(2 0)) star(+ 0.10 * 0.05 ** 0.01) fragment;
eststo clear;
 
/*
sum water_access water22015 i_water2_bf05 water2_bf05 stlit road draig
mws2015 psp2015  ro2015 phi2015  water_repar 
 stlitrepar roadrepar draigrepar water2_gp mwsgp phigp pspgp rogp stlitgp roadgp draiggp
 D_hou D_toilt D_nrgwage D_nrgpri D_agri D_bore D_oap D_dwp D_php D_shed;*/;

save  "${datapath}\hhdata.dta",replace;

